<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Common Controller
 */
class My_Controller extends CI_Controller {
	protected $check_login = true;//是否判断登录
	protected $check_permis = true;//是否判断权限
	public $pagesize = 10;
    private $assign;
	/**
	 * @author Tujt 2016-03-28
	 */
	public function __construct() {
		parent::__construct();
        $this->vendor('logic/PermissionLogic','permis_logic');
        //判断登录
        if($this->check_login && !$this->permis_logic->is_login()){//未登录
            redirect('admin/Login/index');die;
        }
        if($this->check_permis){
            //只控制get访问，post访问的方法不做权限控制
            if('get' == strtolower($_SERVER['REQUEST_METHOD'])){
                //获取菜单，当前页面不在菜单列表中， 就不判断权限
                $this->load->model('Menu_model','menu_model');
                $menus = $this->menu_model->getAll();
                $router = $this->permis_logic->getRouter();
                $currpage = strtolower($router['group'].'/'.$router['controller'].'/'.$router['method']);
                $f = false;
                foreach ($menus as $menu){
                    if(strtolower($menu['url']) == $currpage){
                        $f = true;break;
                    }
                }
                if($f){//在菜单列表中，才进行权限判断
                    $permis = $this->permis_logic->checkPermission();
                    if('yes' != $permis){
                        show_error('对不起，您没有当前页面的权限，请联系管理员设置当前页面的权限。');die;
                    }
                }else{//在控制器下页面没有加入菜单中的情况，只需要有控制器权限，就可以进入控制器下所有的页面
                    $currpage = strtolower($router['group'].'/'.$router['controller'].'/index');
                    $permis = $this->permis_logic->checkPermission($currpage);
                    if('yes' != $permis){
                        show_error('对不起，您没有当前页面的权限，请联系管理员设置当前页面的权限。');die;
                    }
                }
            }
        }
        if(method_exists($this,'init')){//默认执行的方法
            $this->init();
        }
	}
	/**
	 * ajax返回值
	 * @author Toby.Tu 2016-03-31
	 */
	protected function ajaxReturn($data,$code=1) {
	    if(!$code) $code = 200;
	    header('Access-Control-Allow-Origin: *');//跨域
		echo json_encode(array('code'=>$code,'data'=>$data));die;
	}
	/**
	 * 导入类库
	 * @author Toby.tu 2016-08-31
	 */
	public function vendor($clsname,$nickname=''){
        $clsname = strtr($clsname,['@.'=>'models/','$.'=>'logic/']);
        if(empty($nickname)){
            $clsarr = explode('/', $clsname);
            $nickname = strtolower($clsarr[count($clsarr)-1]);
        }
		$this->load->model('../'.$clsname,$nickname);
	}
	/**
	 * 加载分页事件
	 * @author Toby.tu 2016-09-01
	 */
	public function paginate($cur_page,$total_rows=0,$url=''){
		if(empty($total_rows)) return array();
		if(empty($url)){
			$router = $this->permis_logic->getRouter();
			$url = $router['group'].'/'.$router['controller'].'/'.$router['method'];
		}
		$this->load->library('pagination');
		if(isset($this->config->config['pagination']))
			$config = $this->config->config['pagination'];
		else
			$config = array();
        $config['base_url'] = base_url().$url;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $this->pagesize;
        if($cur_page > 1){
        	$config['cur_page'] = $cur_page;
        	$_GET['per_page'] = $config['cur_page'];
        }
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
	}
	/**
     * 添加参数
     * @author Toby.tu 2016-10-21
     */
    public function assign($key,$val){
        $this->assign[$key] = $val;
    }
    /**
     * 加载view模板
     * @author Toby.tu 2016-10-21
     */
    public function view($file=''){
    	if(empty($file)){
    		$file = $this->router->fetch_directory().$this->router->fetch_class().
    					'/'.$this->router->fetch_method();
    	}
        $file = strtolower($file);
        $this->load->view($file,$this->assign);
    }

}
